﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace VersionStar.App
{
    public partial class Patch
    {
        public string FileName { get; set; }
        public string Description { get; set; }
        public string Script { get; set; }

        public void SetFileNameAndLoadScript(string scriptPath)
        {
            string filter = patchNumber + "*.sql";
            List<string> fileNames = Directory.EnumerateFiles(scriptPath, filter).ToList();

            if (fileNames.Count == 0)
            {
                throw new InvalidOperationException(string.Format("Could not find script file: {0} at {1}", filter, scriptPath));
            }

            else if (fileNames.Count > 1)
            {
                throw new InvalidOperationException(
                    string.Format(
                        "Found multiple script files for patch: {0} at {1}: {2}",
                        filter,
                        scriptPath,
                        string.Join(",", fileNames)));
            }

            FileName = fileNames[0];

            Script = File.ReadAllText(FileName, Encoding.Default);
        }
    }
}